<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="600px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="108px" :rules="formRules">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="分类" prop="cid">
                    <el-select v-model="formData.cid" clearable placeholder="请选择分类">
                        <el-option v-for="item in giftCateList" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="类型" prop="type">
                    <el-select v-model="formData.type" clearable placeholder="请选择类型">
                        <el-option label="普通" :value="1" />
                        <el-option label="豪华" :value="2" />
                        <el-option label="尊贵" :value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item label="图片/音乐" prop="file_type">
                    <el-radio-group v-model="formData.file_type">
                        <el-radio-button label="图片" :value="1" />
                        <el-radio-button label="音乐" :value="2" />
                    </el-radio-group>
                </el-form-item>
                <el-form-item :label="formData.file_type == 1 ? '图片' : '音乐'" prop="file_url">
                    <material-picker v-model="formData.file_url" :limit="1" :type="formData.file_type == 1 ? 'image' : 'audio'" @change="handleChange"/>
                    
                    <div v-if="formData.file_type == 2" class="text-gray-500 mt-1" style="font-size: 10px !important;">
                        支持格式：mp3,.wav,.ogg,.aac,.flac,.m4a，文件大小：不超过10MB
                    </div>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input-number v-model="formData.sort" clearable placeholder="请输入排序" type="number" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="roomEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { sacrificeAdd, sacrificeEdit, sacrificeDetail, sacrificeCateLists } from '@/api/sacrificeManage'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import { ref, computed, onMounted, reactive, shallowRef } from 'vue'

// 定义配置项接口


// 定义配置对象接口

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑祭品' : '新增祭品'
})


// 表单数据
const formData = reactive({
    cid: '',
    name: '',
    type: 1,
    file_type: 1,
    file_url: '',
    sort: 0,
})

// 表单验证
const formRules = reactive<any>({
    name: [{
        required: true,
        message: '请输入祭品名称',
        trigger: ['blur']
    }],
    cid: [{
        required: true,
        message: '请选择分类',
        trigger: ['blur']
    }],
    file_url: [{
        required: true,
        message: '请上传图片/音乐',
        trigger: ['blur']
    }]
})



// 在组件挂载时初始化配置项
onMounted(() => {
    
})

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    editId.value = data.id
}
const editId = ref(0)

const getDetail = async (row: Record<string, any>) => {
    const data = await sacrificeDetail({
        id: row.id
    })
    editId.value = row.id
    setFormData(data)
}

// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    mode.value == 'edit' 
        ? await sacrificeEdit({...data, id: editId.value}) 
        : await sacrificeAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
    getGiftCateList()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}

const giftCateList = ref<any[]>([])
const getGiftCateList = async () => {
    const data = await sacrificeCateLists({
        page_no: 1,
        page_size: 10000,
        status: 1
    })
    giftCateList.value = data.lists.map((item: any) => ({
        label: item.name,
        value: item.id
    }))
}

const handleChange = (val: any) => {
    console.log(val)
}
defineExpose({
    open,
    setFormData,
    getDetail
})
</script>

<style scoped>
.config-form {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.config-item {
    display: flex;
    gap: 10px;
    align-items: center;
}

.config-key-input {
    width: 200px;
}

.config-value-input {
    flex: 1;
}
</style>
